package atmgui;

import atm.FileUtil;
import atm.User;
import sk7.SuperArray;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
 * 转账
 */

public class TransferPanel extends JPanel implements ActionListener {
    JTextField usrTextField = new JFormattedTextField();//账户
    JTextField usrTextField1 = new JFormattedTextField();//金额
    private ATM05 atm05;
    private Font font = new Font("宋体", Font.PLAIN, 25);
    private JButton surebtn = new JButton(new ImageIcon("src/images/buttonSure.jpg"));
    private JButton offbtn = new JButton(new ImageIcon("src/images/buttonOff.jpg"));

    public TransferPanel(ATM05 atm05) {


        JLabel label = new JLabel("请输入对方账户");
        label.setBounds(100, 200, 300, 50);
        label.setFont(font);//字体大小
        label.setForeground(Color.BLUE);//字体颜色


        JLabel label1 = new JLabel("请输入金额");
        label1.setBounds(100, 300, 300, 50);
        label1.setFont(font);//字体大小
        label1.setForeground(Color.BLUE);//字体颜色

        //文本框组件

        usrTextField.setBounds(300, 200, 300, 50);


        usrTextField1.setBounds(300, 300, 300, 50);

        surebtn.setBounds(250, 400, 140, 50);
        surebtn.addActionListener(this);


        offbtn.setBounds(500, 400, 140, 50);
        offbtn.addActionListener(this);

        this.add(offbtn);


        this.add(surebtn);
        this.add(label);
        this.add(usrTextField);
        this.add(label1);
        this.add(usrTextField1);


        this.atm05 = atm05;
        this.setLayout(null);
        this.setOpaque(false);


    }

    @Override
    public void actionPerformed(ActionEvent e) {
        User loginUser = atm05.getLoginUser();

        if (e.getSource() == surebtn) {
            try {
                if (Double.parseDouble(usrTextField1.getText()) > loginUser.getBlance()) {
                    JOptionPane.showMessageDialog(this, "余额不足");
                    return;
                } else if (usrTextField.getText().equals(loginUser.getAccount())) {
                    JOptionPane.showMessageDialog(this, "不能转给自己");
                    return;
                }

                ArrayList users = atm05.getUsers();
                for (int i = 0; i < users.size(); i++) {

                    User otherUser = (User) users.get(i);

                    if (otherUser.getAccount().equals(usrTextField.getText())) {
                        loginUser.setBlance(loginUser.getBlance() - Double.parseDouble(usrTextField1.getText()));
                        otherUser.setBlance(otherUser.getBlance() + Double.parseDouble(usrTextField1.getText()));
                        FileUtil.writeObject(atm05.getUsers(), ATM05.FILE_PATH);
                        JOptionPane.showMessageDialog(this, "转账成功");
                        usrTextField.setText("");
                        usrTextField1.setText("");

                        atm05.showPanel("menu");
                        return;
                    }

                }

            }catch (Exception e1){
                JOptionPane.showMessageDialog(TransferPanel.this,"金额输入有误，转账失败");
            }
            }
        else if (e.getSource()==offbtn){
            atm05.showPanel("menu");
        }

        }
    }

